/**
 * Copyright (c) 2014, 2017, Oracle and/or its affiliates.
 * The Universal Permissive License (UPL), Version 1.0
 */
"use strict";
define(["ojs/ojcore","jquery","knockout","ojs/ojdatasource-common","ojs/ojmodel","ojs/ojknockout-model"],function(a,g,b){a.$b=function(a){this.fb=a;this.current=0;this.Init();this.ni=[];this.IW(10)};o_("CollectionPagingDataSource",a.$b,a);a.b.sa(a.$b,a.Fm,"oj.CollectionPagingDataSource");a.$b.prototype.Init=function(){a.$b.u.Init.call(this)};a.b.g("CollectionPagingDataSource.prototype.Init",{Init:a.$b.prototype.Init});a.$b.prototype.UT=function(){return this.nn()?this.Zq:this.totalSize()-this.current};
a.$b.prototype.fw=function(){this.ni=Array(this.UT());var a=this;return this.fb.Fx(this.current,this.current+this.ni.length).then(function(b){for(var e=0;e<b.length;e++)a.ni[e]=b[e];a.xE();a.Qk=a.oa+a.ni.length-1})};a.$b.prototype.xE=function(){if(void 0!==this.Tn){this.Tn.removeAll();for(var b=0;b<this.ni.length;b++)this.Tn.push(a.ib.map(this.ni[b]))}};a.$b.prototype.nr=function(){return this.ni};a.b.g("CollectionPagingDataSource.prototype.getWindow",{nr:a.$b.prototype.nr});a.$b.prototype.BF=function(){void 0===
this.Tn&&(this.Tn=b.observableArray(),this.xE());return this.Tn};a.b.g("CollectionPagingDataSource.prototype.getWindowObservable",{BF:a.$b.prototype.BF});a.$b.prototype.getPage=function(){return this.ge};a.b.g("CollectionPagingDataSource.prototype.getPage",{getPage:a.$b.prototype.getPage});a.$b.prototype.setPage=function(b,d){d=d||{};b=parseInt(b,10);try{a.$b.u.handleEvent.call(this,a.bd.O.BEFOREPAGE,{page:b,previousPage:this.ge})}catch(e){return Promise.reject(null)}this.pageSize=null!=d.pageSize?
d.pageSize:this.pageSize;d.startIndex=b*this.pageSize;var f=this.ge;this.ge=b;this.oa=d.startIndex;var g=this;return new Promise(function(b,c){g.Gg(d).then(function(){a.$b.u.handleEvent.call(g,a.bd.O.PAGE,{page:g.ge,previousPage:f});b(null)},function(){g.ge=f;g.oa=g.ge*g.pageSize;c(null)})})};a.b.g("CollectionPagingDataSource.prototype.setPage",{setPage:a.$b.prototype.setPage});a.$b.prototype.getStartItemIndex=function(){return this.oa};a.b.g("CollectionPagingDataSource.prototype.getStartItemIndex",
{getStartItemIndex:a.$b.prototype.getStartItemIndex});a.$b.prototype.getEndItemIndex=function(){return this.Qk};a.b.g("CollectionPagingDataSource.prototype.getEndItemIndex",{getEndItemIndex:a.$b.prototype.getEndItemIndex});a.$b.prototype.getPageCount=function(){var a=this.totalSize();return-1==a?-1:Math.ceil(a/this.pageSize)};a.b.g("CollectionPagingDataSource.prototype.getPageCount",{getPageCount:a.$b.prototype.getPageCount});a.$b.prototype.fetch=function(a){var b=a||{};if(void 0!==b.pageSize&&void 0!==
b.startIndex){if(!this.nn())return this.WK(null),Promise.resolve();this.Zq=b.startIndex+b.pageSize;var e=this;return this.fw().then(function(){e.WK(null)})}return this.Gg(a)};a.b.g("CollectionPagingDataSource.prototype.fetch",{fetch:a.$b.prototype.fetch});a.$b.prototype.Gg=function(a){var b=a||{};void 0!==b.startIndex&&(this.current=b.startIndex);void 0!==b.pageSize&&(this.Zq=this.pageSize=b.pageSize);var e=this;return new Promise(function(a){try{e.fb.fetch({success:function(){e.fw().then(function(){e.WK(b);
a({data:e.nr(),startIndex:e.current})})}})}catch(c){e.fw().then(function(){e.WK(b);a({data:e.nr(),startIndex:e.current})})}})};a.$b.prototype.WK=function(a){a=a||{};a.silent||this.handleEvent("sync",{data:this.nr(),startIndex:this.current});a.success&&a.success()};a.$b.prototype.handleEvent=function(b,d){return a.$b.u.handleEvent.call(this,b,d)};a.$b.prototype.nn=function(){return this.current+this.Zq<this.totalSize()};a.$b.prototype.IW=function(a){this.Zq=this.pageSize=a};a.$b.prototype.size=function(){var a=
this.nr();return a?a.length:0};a.b.g("CollectionPagingDataSource.prototype.size",{size:a.$b.prototype.size});a.$b.prototype.totalSize=function(){return this.fb.length};a.b.g("CollectionPagingDataSource.prototype.totalSize",{totalSize:a.$b.prototype.totalSize});a.$b.prototype.totalSizeConfidence=function(){return"actual"};a.b.g("CollectionPagingDataSource.prototype.totalSizeConfidence",{totalSizeConfidence:a.$b.prototype.totalSizeConfidence});a.$b.prototype.getCapability=function(){return null};a.b.g("CollectionPagingDataSource.prototype.getCapability",
{getCapability:a.$b.prototype.getCapability})});